
//
//  CommunityTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/10.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "CommunityTableView.h"
#import "UserModel.h"

@interface CommunityTableView()
@property(nonatomic,copy)UserModel *userModel;

@property(nonatomic,copy)NSArray *hotTagsArray;

@property(nonatomic,copy)NSArray *hotTopicsArray;

@property(nonatomic,copy)NSArray *recommandUserArray;

@property(nonatomic,copy)NSMutableArray *allBiaoQianArray;
@end

@implementation CommunityTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self=[super initWithFrame:frame style:style]) {
        self.showsVerticalScrollIndicator=NO;
        self.delegate=self;
        self.dataSource=self;
        _dataArray=[NSMutableArray array];
        self.tableFooterView=[UIView new];
        self.tableHeaderView=[self createTableHeaderView];
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator=NO;
        self.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getHttpData];
        }];
        [self getHttpData];
        
    }
    return self;
}
//创建tableHeaderView
-(UIView *)createTableHeaderView
{
    NSLog(@".........................");
    UIView *headerview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 172+75+50)];
    //view.backgroundColor=[UIColor colorWithRed:145/255.0 green:208/255.0 blue:234/255.0 alpha:1];
//    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 120)];
//    backgroundView.backgroundColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
//    [headerview addSubview:backgroundView];
    
    
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, headerview.frame.size.width, 209)];
    imgView.backgroundColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
    imgView.image=[UIImage imageNamed:@"community-backgroundImage.png"];
    [headerview addSubview:imgView];
    
    //用户头像
    UIImageView *userHeadImageView=[[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-60)/2.0, 25, 60, 60)];
    [userHeadImageView sd_setImageWithURL:[NSURL URLWithString:_userModel.user_profile[@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1.png"]];
    userHeadImageView.layer.cornerRadius=userHeadImageView.frame.size.width/2.0;
    userHeadImageView.layer.masksToBounds=YES;
    //[userHeadImageView setImageWithURL:[NSURL URLWithString:_model.userHeadImageUrl] placeholderImage:[UIImage imageNamed:@"community-backgroundImage.png"]];
    [headerview addSubview:userHeadImageView];
    
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:_userModel.user_profile[@"avatar_url"]]];
    UIImage *myHeadImage=[UIImage imageWithData:data];
    userHeadImageView.image=myHeadImage;
    
    
    
    
    
    UIButton *userHeadBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    userHeadBtn.frame=CGRectMake((self.frame.size.width-60)/2.0, 25, 60, 60);
    userHeadBtn.tag=[_userModel.userId integerValue];
    
//    //[userHeadBtn setBackgroundImage:[UIImage imageNamed:@"2012729106016418.jpg"] forState:UIControlStateNormal];
//    [userHeadBtn setBackgroundImage:userHeadImageView.image forState:UIControlStateNormal];
//    userHeadBtn.layer.cornerRadius=userHeadBtn.frame.size.width/2.0;
//    userHeadBtn.layer.masksToBounds=YES;
    [userHeadBtn addTarget:self action:@selector(userHeadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:userHeadBtn];
    
    
    
    //性别
    UIImageView *sexImageView=[[UIImageView alloc]init];
    sexImageView.frame=CGRectMake((self.frame.size.width-20)/2.0, CGRectGetMaxY(userHeadBtn.frame), 20, 20);
    sexImageView.image=[UIImage imageNamed:@"sex_woman"];
    NSInteger sex=[_userModel.user_profile[@"sex"] integerValue];
    if (sex==0) {
        sexImageView.image=[UIImage imageNamed:@"sex_man"];
    }else{
        sexImageView.image=[UIImage imageNamed:@"sex_woman"];
    }
    [headerview addSubview:sexImageView];
    
    
    
    
    //用户名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(userHeadBtn.frame), sexImageView.frame.origin.x-10, 20)];
    name.textAlignment=NSTextAlignmentRight;
    name.text=_userModel.userName;
    name.font=[UIFont systemFontOfSize:15];
    name.textColor=[UIColor whiteColor];
    name.frame=CGRectMake(CGRectGetMaxX(name.frame)-[self calculateWidthWithLable:name], name.frame.origin.y, [self calculateWidthWithLable:name], name.frame.size.height);
    [headerview addSubview:name];
    
    if ([_userModel.userName isKindOfClass:[NSNull class]]) {
        name.text=@"= =";
    }else{
        NSString *myName=[NSString stringWithFormat:@"%@",_userModel.userName];
        if (myName.length==0) {
            name.text=@"= =";
        }
    }
    
    
    
    //账号
    UILabel *userAccount=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(sexImageView.frame)+10, name.frame.origin.y, self.frame.size.width-CGRectGetMaxX(sexImageView.frame)-10, 20)];
    userAccount.text=_userModel.userPhone;
//                            userAccount.text=_model.userAccount;
    userAccount.textColor=[UIColor whiteColor];
    userAccount.font=[UIFont systemFontOfSize:11];
    [headerview addSubview:userAccount];
    
    if ([_userModel.userPhone isKindOfClass:[NSNull class]]) {
        userAccount.text=@"未知账号";
    }else{
        NSString *myName=[NSString stringWithFormat:@"%@",_userModel.userPhone];
        if (myName.length==0) {
            userAccount.text=@"未知账号";
        }
    }
    
    
    
    
    //年龄
    UILabel *age=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(name.frame), (self.frame.size.width-5)/2.0, 20)];
    if ([_userModel.user_profile[@"age"] isKindOfClass:[NSNull class]]) {
        age.text=@"";
    }else{
        if ([[NSString stringWithFormat:@"%@岁",_userModel.user_profile[@"age"]] isEqualToString:@" "]) {
            NSLog(@"年龄是空=====%@",[NSString stringWithFormat:@"%@",_userModel.user_profile[@"age"]]);
        }else{
             NSLog(@"年龄不是空====%@",[NSString stringWithFormat:@"%@",_userModel.user_profile[@"age"]]);
        }
        age.text=[NSString stringWithFormat:@"%@岁",_userModel.user_profile[@"age"]];
    }
    if ([_userModel.user_profile[@"age"] isKindOfClass:[NSNull class]]) {
        age.text=@"0岁";
    }else{
        age.text=[NSString stringWithFormat:@"%@岁",_userModel.user_profile[@"age"]];
        NSString *myAge=[NSString stringWithFormat:@"%@",_userModel.user_profile[@"age"]];
        NSLog(@"长度%ld",myAge.length);
        if ([myAge isEqualToString:@""]) {
            NSLog(@"相等======");
            age.text=@"0岁";
        }else{
            NSLog(@"不相等======");
            age.text=[NSString stringWithFormat:@"%@岁",_userModel.user_profile[@"age"]];
        }
    }

    
//                              age.text=[NSString stringWithFormat:@"%ld",_model.age];
    age.textAlignment=NSTextAlignmentRight;
    age.textColor=[UIColor whiteColor];
    age.font=[UIFont systemFontOfSize:11];
    [headerview addSubview:age];
//    if ([_userModel.userPhone isKindOfClass:[NSNull class]]||userAccount.text.length==0) {
//        userAccount.hidden=YES;
//        age.frame=CGRectMake(userAccount.frame.origin.x, userAccount.frame.origin.y, age.frame.size.width, age.frame.size.height);
//        age.textAlignment=NSTextAlignmentLeft;
//    }
    
    
    
    
    
    //地区
    UILabel *addressLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(age.frame)+10, age.frame.origin.y, self.frame.size.width-(CGRectGetMaxX(age.frame)+10), 20)];
    addressLab.text=[NSString stringWithFormat:@"%@",_userModel.user_profile[@"city"]];
//                              addressLab.text=_model.userAddress;
    addressLab.textColor=[UIColor whiteColor];
    addressLab.font=[UIFont systemFontOfSize:11];
    [headerview addSubview:addressLab];
    
    if ([_userModel.user_profile[@"city"] isKindOfClass:[NSNull class]]) {
        addressLab.text=@"未知";
    }else{
        NSString *myName=[NSString stringWithFormat:@"%@",_userModel.user_profile[@"city"]];
        if (myName.length==0) {
            addressLab.text=@"未知";
        }
    }
    
    
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(sexImageView.frame.origin.x-50, CGRectGetMaxY(age.frame), 30, 20)];
    biaoqian.text=@"标签";
    biaoqian.textColor=[UIColor whiteColor];
    biaoqian.font=[UIFont systemFontOfSize:11];
    [headerview addSubview:biaoqian];
//    if ([_userModel.tags count]==0) {
//        biaoqian.hidden=YES;
//    }
    if ([_userModel.tags count]==0) {
        //biaoqian.hidden=YES;
        biaoqian.text=@"标签:   ==       ==";
        biaoqian.frame=CGRectMake(sexImageView.frame.origin.x-50, CGRectGetMaxY(age.frame), self.frame.size.width-(sexImageView.frame.origin.x-50), 20);
    }
    _allBiaoQianArray=[NSMutableArray array];
    
    NSArray *biaoqianArr=_userModel.tags;
    CGFloat biaoqian_w = 0.0;
    
    for (int i=0; i<biaoqianArr.count; i++) {
        if (i<2) {
            //标签1
            UILabel *biaoqian1=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(biaoqian.frame)+2+biaoqian_w+15*i, biaoqian.frame.origin.y, 0, 20)];
            biaoqian1.text=[NSString stringWithFormat:@"%@",biaoqianArr[i][@"name"]];
            [_allBiaoQianArray addObject:[NSString stringWithFormat:@"%@",biaoqianArr[i][@"name"]]];
            biaoqian1.textAlignment=NSTextAlignmentCenter;
            biaoqian1.font=[UIFont systemFontOfSize:11];
            biaoqian1.textColor=[UIColor whiteColor];
            biaoqian1.frame=CGRectMake(CGRectGetMaxX(biaoqian.frame)+2+biaoqian_w+15*i, biaoqian.frame.origin.y, [self calculateWidthWithLable:biaoqian1], 20);
            [headerview addSubview:biaoqian1];
            biaoqian_w+=[self calculateWidthWithLable:biaoqian1];
        }
    }
    
    if (_userModel.tags.count>0) {
        //更多标签
        UIImageView *moreBiaoQianImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(biaoqian.frame)+2+biaoqian_w+biaoqianArr.count*15, biaoqian.frame.origin.y, 20, 20)];
        moreBiaoQianImageView.image=[UIImage imageNamed:@"baritem_left_hl"];
        [headerview addSubview:moreBiaoQianImageView];
        
        //盖按钮
        UIButton *biaoqianBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        biaoqianBtn.frame=CGRectMake(CGRectGetMaxX(biaoqian.frame), biaoqian.frame.origin.y, self.frame.size.width-CGRectGetMaxX(biaoqian.frame), moreBiaoQianImageView.frame.size.height);
        [biaoqianBtn addTarget:self action:@selector(moreBiaoQianBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [headerview addSubview:biaoqianBtn];
    }
    
    
    //个性签名
    UILabel *personalName=[[UILabel alloc]initWithFrame:CGRectMake(biaoqian.frame.origin.x-30, CGRectGetMaxY(biaoqian.frame), self.frame.size.width-((biaoqian.frame.origin.x-30))*2, 20)];
    personalName.text=[NSString stringWithFormat:@"个性签名:%@",_userModel.user_profile[@"signatures"]];
    personalName.textColor=[UIColor whiteColor];
    personalName.font=[UIFont systemFontOfSize:11];
    [headerview addSubview:personalName];
    personalName.textAlignment=NSTextAlignmentCenter;
    _myPersonalName=personalName.text;
    
    if ([_userModel.user_profile[@"signatures"] isKindOfClass:[NSNull class]]) {
        //personalName.hidden=YES;
        personalName.text=@"个性签名: = =  ";
    }else{
        NSString *myPersonalName=[NSString stringWithFormat:@"%@",_userModel.user_profile[@"signatures"]];
        if (myPersonalName.length==0) {
            personalName.text=@"个性签名: = =  ";
        }
    }
    
    //显示个性签名详情
    UIButton *allPersonalNameBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    allPersonalNameBtn.frame=personalName.frame;
    [allPersonalNameBtn addTarget:self action:@selector(allPersonalNameBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerview addSubview:allPersonalNameBtn];
    
    //贴吧 互动
    NSArray *arr=@[@{@"image":@"shequ_tieba.png",@"name":@"贴吧"},@{@"image":@"shequ_hudong.png",@"name":@"互动"}];
    for (int i=0; i<arr.count; i++) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(((self.frame.size.width-1)/2.0+1)*i, CGRectGetMaxY(imgView.frame), (self.frame.size.width-1)/2.0, 61)];
        view.backgroundColor=[UIColor whiteColor];
        [headerview addSubview:view];
        
        //图片
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width/2.0-(40-3*2), 8, 45, 45)];
        imageView.image=[UIImage imageNamed:arr[i][@"image"]];
        [view addSubview:imageView];
        
        //名字
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+10, 8, view.frame.size.width-CGRectGetMaxX(imageView.frame)-10, 51-3*2)];
        lab.text=arr[i][@"name"];
        lab.font=[UIFont systemFontOfSize:16];
        lab.textColor=[UIColor blackColor];
        [view addSubview:lab];
        
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=view.frame;
        btn.tag=i+1;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [headerview addSubview:btn];
    }
    
    //热门话题
    UIView *hotView=[self getSectionView];
    hotView.frame=CGRectMake(0, 130+40+5+75+11+10, hotView.frame.size.width, hotView.frame.size.height);
    [headerview addSubview:hotView];
    NSArray *hotArray=[NSArray arrayWithArray:_hotTagsArray];
    /*
     * hotArray=_model.hotTopicArray;
     */
   
    CGFloat hot_w=(self.frame.size.width-1)/2.0;
    CGFloat hot_h=50;
    NSLog(@"长度======%ld",_hotTagsArray.count);
    if (_hotTagsArray.count==4) {
        for (int i=0; i<2; i++) {
            for (int j=0; j<2; j++) {
                UILabel *hotLab=[[UILabel alloc]initWithFrame:CGRectMake((hot_w+1)*j, CGRectGetMaxY(hotView.frame)+0.5+(hot_h+1)*i, hot_w, hot_h)];
                
                //盖按钮
                UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame=hotLab.frame;
                btn.tag=i*2+j+1;
                [btn setTitle:[NSString stringWithFormat:@"#%@#",hotArray[i*2+j][@"name"]] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                btn.titleLabel.font=[UIFont systemFontOfSize:15];
                btn.backgroundColor=[UIColor whiteColor];
                [btn addTarget:self action:@selector(hotViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                [headerview addSubview:btn];
            }
            
        }
         headerview.frame=CGRectMake(headerview.frame.origin.x, headerview.frame.origin.y, headerview.frame.size.width, CGRectGetMaxY(hotView.frame)+1+(hot_h+1)*2+15);
    }else{
         headerview.frame=CGRectMake(headerview.frame.origin.x, headerview.frame.origin.y, headerview.frame.size.width, CGRectGetMaxY(hotView.frame)+1+(hot_h+1)*0+15);
    }
   
    return headerview;
}
//更多标签按钮点击事件
-(void)moreBiaoQianBtnClick:(UIButton *)sender
{
    [self.myDelegate moreBiaoQianBtnClick:_allBiaoQianArray];
}

//个性签名完整显示
-(void)allPersonalNameBtnClick:(UIButton *)sender
{
    [self.myDelegate allPersonalNameBtnClick:_myPersonalName];
}

//贴吧 互动按钮点击事件
-(void)btnClick:(UIButton *)sender
{
    // NSLog(@"...........========%ld",sender.tag);
    [self.myDelegate clickBtnAtIndex:sender.tag];
}

//代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_arr[section] count];
    //return _dataArray[section].count;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _arr.count;
    //return _dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    if (_arr!=nil) {
        if (indexPath.section==0) {
            //用户头像
            UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, (70-40)/2.0, 40, 40)];
            // headImageView.image=[UIImage imageNamed:_arr[indexPath.section][@"arr"][indexPath.row][@"headImage"]];
            [headImageView sd_setImageWithURL:[NSURL URLWithString:_arr[indexPath.section][indexPath.row][@"user_info"][@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1.png"]];
            headImageView.layer.cornerRadius=40/2.0;
            headImageView.layer.masksToBounds=YES;
            [cell.contentView addSubview:headImageView];
            
            //分割线
            UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 71.5, self.frame.size.width, 0.5)];
            lineView.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
            [cell.contentView addSubview:lineView];
            
            
            //热门帖子
            CGFloat height=56/3.0;//高度
            //姓名
            UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 5, 50, height)];
            //name.backgroundColor=[UIColor grayColor];
            name.text=_arr[indexPath.section][indexPath.row][@"user_info"][@"nikename"];
            //name.text=_model.hotInvitationArray[indexPath.row][@"name"];
            name.font=[UIFont systemFontOfSize:13];
            name.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
            name.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 5+2, [self calculateWidthWithLable:name], height);
            [cell.contentView addSubview:name];
            
            
            //标签
            UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame)+2, 100, height)];
            //label.backgroundColor=[UIColor grayColor];
            label.text=_arr[indexPath.section][indexPath.row][@"tag_name"];
            // label.text=_model.hotInvitationArray[indexPath.row][@"biaoqian"];
            label.font=[UIFont systemFontOfSize:12];
            label.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
            if ([self calculateWidthWithLable:label]>self.frame.size.width-CGRectGetMaxX(headImageView.frame)-2) {
                label.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame), self.frame.size.width-CGRectGetMaxX(headImageView.frame)-2, height);
            }else{
                label.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame), [self calculateWidthWithLable:label]+8, height);
            }
            NSLog(@"标签的宽度=====%f",label.frame.size.width);
            label.layer.borderColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1].CGColor;
            label.layer.borderWidth=1.0;
            label.layer.cornerRadius=2.0;
            label.textAlignment=NSTextAlignmentCenter;
            [cell.contentView addSubview:label];
            
            //帖子标题
            UILabel *topic=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(label.frame)+2, 100, height)];
            //topic.backgroundColor=[UIColor grayColor];
            topic.text=_arr[indexPath.section][indexPath.row][@"title"];
            //topic.text=_model.hotInvitationArray[indexPath.row][@"topic"];
            topic.font=[UIFont systemFontOfSize:13];
            topic.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
            if ([self calculateWidthWithLable:topic]>self.frame.size.width-CGRectGetMaxX(headImageView.frame)-2) {
                topic.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+6, CGRectGetMaxY(label.frame), self.frame.size.width-CGRectGetMaxX(headImageView.frame)-2, height);
            }else{
                topic.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8 , CGRectGetMaxY(label.frame), [self calculateWidthWithLable:topic], height);
            }
            [cell.contentView addSubview:topic];

        }else if (indexPath.section==1){
            
            UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, (70-40)/2.0, 40, 40)];
            // headImageView.image=[UIImage imageNamed:_arr[indexPath.section][@"arr"][indexPath.row][@"headImage"]];
            [headImageView sd_setImageWithURL:[NSURL URLWithString:_arr[indexPath.section][indexPath.row][@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1.png"]];
            headImageView.layer.cornerRadius=40/2.0;
            headImageView.layer.masksToBounds=YES;
            [cell.contentView addSubview:headImageView];
            
            //分割线
            UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 71.5, self.frame.size.width, 0.5)];
            lineView.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
            [cell.contentView addSubview:lineView];
            
            //推荐用户shequ_tebietuijianyonghu
            CGFloat height=(56-8)/3.0;//高度
            //姓名
            UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 2+headImageView.frame.origin.y, 50, height)];
            //name.backgroundColor=[UIColor grayColor];
            name.text=_arr[indexPath.section][indexPath.row][@"nikename"];
            //name.text=_model.hotInvitationArray[indexPath.row][@"name"];
            name.font=[UIFont systemFontOfSize:13];
            name.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
            name.frame=CGRectMake(name.frame.origin.x, name.frame.origin.y, [self calculateWidthWithLable:name], height);
            [cell.contentView addSubview:name];
            
            //性别
            UIImageView *sexImageView=[[UIImageView alloc]init];
            sexImageView.frame=CGRectMake(CGRectGetMaxX(name.frame)+3, name.frame.origin.y, height, height);
            NSInteger sexState=[_arr[indexPath.section][indexPath.row][@"sex"] integerValue];
            if (sexState==0) {
                sexImageView.image=[UIImage imageNamed:@"sex_man.png"];
            }else{
                sexImageView.image=[UIImage imageNamed:@"sex_woman.png"];
            }
            [cell.contentView addSubview:sexImageView];
            
            //年龄
            UILabel *age=[[UILabel alloc]initWithFrame:CGRectMake(name.frame.origin.x, CGRectGetMaxY(name.frame)+4, 60, height)];
            if ([_arr[indexPath.section][indexPath.row][@"age"] isKindOfClass:[NSNull class]]) {
                age.text=@"0岁";
            }else{
                age.text=[NSString stringWithFormat:@"%@岁",_arr[indexPath.section][indexPath.row][@"age"]];
                NSString *myAge=[NSString stringWithFormat:@"%@",_arr[indexPath.section][indexPath.row][@"age"]];
                NSLog(@"长度%ld",myAge.length);
                if ([myAge isEqualToString:@""]) {
                    NSLog(@"相等======");
                    age.text=@"0岁";
                }else{
                    NSLog(@"不相等======");
                    age.text=[NSString stringWithFormat:@"%@岁",_arr[indexPath.section][indexPath.row][@"age"]];
                }
            }
            age.textColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1];
            age.font=[UIFont systemFontOfSize:11];
            [cell.contentView addSubview:age];
            
            
            //关注
            //关注 加好友 更多
            CGFloat addbtn_w=60;
            CGFloat addbtn_h=20;
            UIButton *addBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
            addBtn1.frame=CGRectMake(self.frame.size.width-addbtn_w-20, (71.5-addbtn_h)/2.0, addbtn_w, addbtn_h);
            [addBtn1 setTitle:@"+关注" forState:UIControlStateNormal];
            addBtn1.titleLabel.font=[UIFont systemFontOfSize:13];
            addBtn1.backgroundColor=[UIColor colorWithRed:240/255.0 green:156/255.0 blue:38/255.0 alpha:1];
            addBtn1.layer.borderColor=[UIColor colorWithRed:240/255.0 green:156/255.0 blue:38/255.0 alpha:1].CGColor;
            addBtn1.layer.borderWidth=1.0;
            addBtn1.layer.cornerRadius=3.0;
            addBtn1.tag=[_arr[indexPath.section][indexPath.row][@"user_id"] integerValue]+1000000;
            [addBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [addBtn1 addTarget:self action:@selector(addBtn1Click:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:addBtn1];
        }
        
    }
    return cell;
}

//加关注
-(void)addBtn1Click:(UIButton *)sender
{
    [self getAttentionUser:[NSString stringWithFormat:@"%ld",sender.tag-1000000]];
}


//加入热门聊天室
-(void)addInHotRoom:(UIButton *)sender
{
    NSLog(@"加入热门聊天室%ld",sender.tag);
}

//加入特别推荐小组
-(void)addInSpecialTeam:(UIButton *)sender
{
    NSLog(@"加入特别推荐小组%ld",sender.tag);
}

//特别推荐用户添加为好友
-(void)addSpecialUserToFriend:(UIButton *)sender
{
    NSLog(@"特别推荐用户添加为好友%ld",sender.tag);
}
//关注特别推荐用户
-(void)careSpecialUserToFriend:(UIButton *)sender
{
    NSLog(@"关注特别推荐用户%ld",sender.tag);
    if ([sender.titleLabel.text isEqualToString:@"已关注"]) {
        [sender setTitle:@"+关注" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1] forState:UIControlStateNormal];
        sender.layer.borderColor=[UIColor colorWithRed:231/255.0 green:142/255.0 blue:45/255.0 alpha:1].CGColor;
    }else{
        [sender setTitle:@"已关注" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        sender.layer.borderColor=[UIColor grayColor].CGColor;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44.0)];
    view.backgroundColor=[UIColor whiteColor];
    
    
    if (section==0) {
        //图标
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(5, (44.0-18)/2.0, 18, 18)];
        imgView.image=[UIImage imageNamed:@"shequ_hotTopic"];
        [view addSubview:imgView];
        
        //标题
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, 0, self.frame.size.width/2.0-CGRectGetMaxX(imgView.frame), 44.0)];
        lab.text=@"热门帖子";
        lab.font=[UIFont systemFontOfSize:15];
        lab.textColor=[UIColor blackColor];
        [view addSubview:lab];
        //更多
        UILabel *moreLab=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-60, 0, 30, 44.0)];
        moreLab.text=@"更多";
        moreLab.font=[UIFont systemFontOfSize:13];
        moreLab.textAlignment=NSTextAlignmentRight;
        moreLab.textColor=[UIColor blackColor];
        [view addSubview:moreLab];
        
        
        //更多按钮的图标
        UIImageView *moreImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(moreLab.frame), 24.0/2.0, 20, 20)];
        moreImageView.image=[UIImage imageNamed:@"shequ_more.png"];
        [view addSubview:moreImageView];
        [backgroundView addSubview:view];
        
        
        //盖按钮
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=view.frame;
        btn.tag=10+section;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
    }else{
        //图标
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(5, (44.0-18)/2.0, 18, 18)];
        imgView.image=[UIImage imageNamed:@"shequ_tebietuijianyonghu"];
        [view addSubview:imgView];
        
        //标题
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, 0, self.frame.size.width/2.0-CGRectGetMaxX(imgView.frame), 44.0)];
        lab.text=@"特别推荐用户";
        lab.font=[UIFont systemFontOfSize:15];
        lab.textColor=[UIColor blackColor];
        [view addSubview:lab];
        [backgroundView addSubview:view];

    }
   
    return backgroundView;
}
//热门话题View
-(UIView *)getSectionView
{
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 45)];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44.0)];
    view.backgroundColor=[UIColor whiteColor];
    
    //图标
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(5, (45.0-18)/2.0-1, 18, 18)];
    imgView.image=[UIImage imageNamed:@"shequ_remen.png"];
    [view addSubview:imgView];
    
    //标题
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, 0, self.frame.size.width/2.0-CGRectGetMaxX(imgView.frame), 44.0)];
    lab.text=@"热门话题";
    lab.font=[UIFont systemFontOfSize:15];
    lab.textColor=[UIColor blackColor];
    [view addSubview:lab];
    
    //更多
    UILabel *moreLab=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-60, 0, 30, 44.0)];
    moreLab.text=@"更多";
    moreLab.font=[UIFont systemFontOfSize:13];
    moreLab.textAlignment=NSTextAlignmentRight;
    moreLab.textColor=[UIColor blackColor];
    [view addSubview:moreLab];
    
    
    //更多按钮的图标
    UIImageView *moreImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(moreLab.frame), 24.0/2.0, 20, 20)];
    moreImageView.image=[UIImage imageNamed:@"shequ_more.png"];
    [view addSubview:moreImageView];
    
    
    
    //盖按钮
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=view.frame;
    btn.tag=9;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    [backgroundView addSubview:view];
    return backgroundView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72.0;
}

//根据文字计算宽度和高度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.width;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        [self.myDelegate gotoXiangQing:_arr[indexPath.section][indexPath.row][@"id"]];
    }else{
        NSString *userId=[NSString stringWithFormat:@"%@",_arr[indexPath.section][indexPath.row][@"user_id"]];
        [self.myDelegate gotoMyAttentionViewController:userId];
    }
    
    
}

//热门话题点击事件
-(void)hotViewBtnClick:(UIButton *)sender
{
    NSString *str=sender.titleLabel.text;
    NSMutableString *str2=[NSMutableString stringWithString:str];
    [str2 deleteCharactersInRange:[str2 rangeOfString:@"#"]];
    [str2 deleteCharactersInRange:[str2 rangeOfString:@"#"]];
    str=str2;
    NSLog(@"==========%@",str);
    // NSLog(@"++++++++++%@",str2);
    //NSLog(@"点击了热门%ld==%@",sender.tag,sender.titleLabel.text);
    //1,2,3,4
    [self.myDelegate clickHotAtIndex:sender.tag andBiaoQian:str andTagId:_hotTagsArray[sender.tag-1][@"tag_id"]];
}

//用户头像点击事件
-(void)userHeadBtnClick:(UIButton *)sender
{
    NSLog(@"点击头像");
    [self.myDelegate userHeadBtnClick:sender.tag];
}

//获取数据
-(void)getHttpData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [self showCicle:YES];
    [manager GET:[NSString stringWithFormat:@"%@%@",UserInfo,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self showCicle:NO];
        [self.header endRefreshing];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic);
        if (dic) {
            _userModel=[[UserModel alloc]initWithDictionary:dic];
            
           
            if (![_userModel.user_profile[@"avatar_url"] isKindOfClass:[NSNull class]]) {
                 NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
                [user setObject:_userModel.user_profile[@"avatar_url"] forKey:@"userHeadImage"];
                [user synchronize];
            }

            
            [self getFourTag];
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
       // [self isLoading:NO];
        [self showCicle:NO];
        [self.header endRefreshing];
        [self.myDelegate getDataSuccess:false];
    }];
}


//获取4个热门标签 4个热门帖子
-(void)getFourTag
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:CommunityHome,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"得到的四个标签======%@",dic);
        if (dic) {
            _hotTagsArray=[NSArray arrayWithArray:dic[@"community_hot_tags"]];
            
            _hotTopicsArray=[NSArray arrayWithArray:dic[@"hotTopic"]];
            
            _recommandUserArray=[NSArray arrayWithArray:dic[@"recommend_users"]];
            
            _arr=[NSMutableArray array];
            
            [_arr addObject:_hotTopicsArray];
            
            [_arr addObject:_recommandUserArray];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.tableHeaderView=[self createTableHeaderView];
                [self reloadData];
            });
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
}


//关注用户
-(void)getAttentionUser:(NSString *)userId
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager POST:[NSString stringWithFormat:AttentionUser,token,userId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"加关注====%@",dic);
        [[NSNotificationCenter defaultCenter]postNotificationName:@"careUserSuccessMessage" object:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];
    
}

@end




